package admin

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	apiAdmin "miaoyu/api/backend/admin"
	modelAdmin "miaoyu/internal/model/admin"
	"miaoyu/internal/service"
)

var Department = new(departmentCtl)

type departmentCtl struct {
}

func (c *departmentCtl) Index(ctx context.Context, req *apiAdmin.UserDepartmentIndexReq) (res *apiAdmin.UserDepartmentIndexRes, err error) {

	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl": "backend/user/department/index.html",
		"footTpl": "backend/user/department/index.js.html",
		"title":   "部门管理",
	})
	return
}
func (c *departmentCtl) Add(ctx context.Context, req *apiAdmin.UserDepartmentAddReq) (res *apiAdmin.UserDepartmentAddRes, err error) {
	parentId := req.ParentId
	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl":  "backend/user/department/add.html",
		"footTpl":  "backend/user/department/add.js.html",
		"siteName": "控制台",
		"parentId": parentId,
	})

	return
}
func (c *departmentCtl) Edit(ctx context.Context, req *apiAdmin.UserDepartmentEditReq) (res *apiAdmin.UserDepartmentEditRes, err error) {
	resId := req.ResId

	itemRes, resErr := service.Department().GetItemById(ctx, resId)
	if resErr != nil {
		err = resErr
		service.UniResp().UniResp(g.RequestFromCtx(ctx), err.Error(), "")
		return
	}
	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl":  "backend/user/department/add.html",
		"footTpl":  "backend/user/department/edit.js.html",
		"siteName": "控制台",
		"data":     itemRes,
	})

	return
}

func (c *departmentCtl) DoDel(ctx context.Context, req *apiAdmin.UserDepartmentDelDoReq) (res *apiAdmin.UserDepartmentDelDoRes, err error) {
	resId := req.ResId
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.Department().Del(ctx, resId)
	if resErr != nil {
		err = resErr
		commonJson.SetErrorJsonExit(err.Error())
		return
	}
	commonJson.SetSuccessJsonExit("操作成功", nil)
	return

}

func (c *departmentCtl) DoList(ctx context.Context, req *apiAdmin.UserDepartmentListDoReq) (res *apiAdmin.UserDepartmentListDoRes, err error) {

	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))

	listRes, resErr := service.Department().GetAllList(ctx, nil)
	if resErr != nil {
		err = resErr
		return
	}
	commonJson.SetSuccessJsonExit("ok", listRes)

	return
}
func (c *departmentCtl) DoSubmit(ctx context.Context, req *apiAdmin.UserDepartmentSubmitDoReq) (res *apiAdmin.UserDepartmentSubmitDoRes, err error) {
	var submitInput modelAdmin.UserDepartmentSubmitInput
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	err = gconv.Scan(req, &submitInput)
	if err != nil {
		return
	}
	resId := req.ResId
	if resId != 0 {
		_, err = service.Department().Edit(ctx, submitInput, resId)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		} else {
			commonJson.SetSuccessJsonExit("修改成功", nil)
		}
		return
	} else {
		_, err = service.Department().Add(ctx, submitInput)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		}

		commonJson.SetSuccessJsonExit("添加成功", nil)
	}

	return
}
